package org.cttic.visualization.visualization.z_service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.cttic.visualization.visualization.common.EsDataStrip;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.common.module.serviceCountData;
import org.cttic.visualization.visualization.common.module.serviceCountData.*;
import org.cttic.visualization.visualization.util.AggregationUtil;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.cttic.visualization.visualization.z_service.ancillaryServicesCount;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-18 9:45
 * 接口：2.4.3各省ETC门架和收费站的北斗辅助服务次数。
 */
@Service
public class ancillaryServicesCountImpl implements ancillaryServicesCount {
    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    private AggregationUtil aggregationUtil;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private org.cttic.visualization.visualization.util.sixUtil.servicesCountUtil servicesCountUtil;
    @Override
    public String servicesCount(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String provinceId = jsonData.getString("provinceId");
        String queryType = jsonData.getString("queryType");
        serviceCountData serviceCountData = new serviceCountData();
        List<pointTypeArr> list = new ArrayList<>();

        //按全国查
        if(provinceId==null){
            //时间段查询
            if(queryType.equals("1")){
                String queryValue1 = jsonData.getString("queryValue1");
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime = DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);
                String pointType = jsonData.getString("pointType");
                if (pointType.equals("0")){
                    for (Integer provincel : provinceData.provinceList) {
                        pointTypeArr.quotaIn quotaIn1 = new pointTypeArr.quotaIn();
                        pointTypeArr.quotaIn quotaIn2 = new pointTypeArr.quotaIn();
                        pointTypeArr pointTypeArr1 = new serviceCountData.pointTypeArr();
                        pointTypeArr pointTypeArr2 = new serviceCountData.pointTypeArr();

                        //收费站
                        String querySql1="SELECT sum(serviceNums) as serviceNums FROM `serviceCount` WHERE provinceId= '"+ provincel + "'" +
                                " and processingTime<'"+endTime+"' and processingTime>'"+startTime+"' and pointType=3";
                        System.out.println(querySql1);
                        Map<String, Object> map = jdbcTemplate.queryForMap(querySql1);
                        if(map!=null&&map.size()>0) {
                            cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(map);
                            Long serviceNums = jsonObject.getLong("serviceNums");
                            //3-收费站  4-门架/省界门架
                            quotaIn1.setPointType("3");
                            quotaIn1.setServiceNums(serviceNums+"");
                            pointTypeArr1.setProvinceId(provincel+"");
                            pointTypeArr1.setProvinceName(provinceData.provinceID.get(provincel));
                            pointTypeArr1.setQuotaInfo(quotaIn1);
                        }
//收费站
                        String querySql2="SELECT sum(serviceNums) as serviceNums FROM `serviceCount` WHERE provinceId= '"+ provincel + "'" +
                                " and processingTime<'"+endTime+"' and processingTime>'"+startTime+"' and pointType=4";
                        System.out.println(querySql2);
                        Map<String, Object> map2 = jdbcTemplate.queryForMap(querySql2);
                        if(map2!=null&&map2.size()>0) {
                            cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(map2);
                            Long serviceNums = jsonObject.getLong("serviceNums");
                            //3-收费站  4-门架/省界门架
                            quotaIn2.setPointType("4");
                            quotaIn2.setServiceNums(serviceNums+"");
                            pointTypeArr2.setProvinceId(provincel+"");
                            pointTypeArr2.setProvinceName(provinceData.provinceID.get(provincel));
                            pointTypeArr2.setQuotaInfo(quotaIn2);
                        }

                        list.add(pointTypeArr1);
                        list.add(pointTypeArr2);
                    }
                    serviceCountData.setPointTypeArray(list);
                    ObjectMapper mapper = new ObjectMapper();
                    try {
                        String value = mapper.writeValueAsString(serviceCountData);
                        return value;
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }else if (pointType.equals("1")){

                }
                //时间点查询
            }else if(queryType.equals("0")){

            }
        }else if (!provinceId.contains("|")){
            //按单省查
            //时间段查询
            if(queryType.equals("1")){
                String queryValue1 = jsonData.getString("queryValue1");
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime = DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);
                String pointType = jsonData.getString("pointType");
                if (pointType.equals("0")){
                    servicesCountUtil.servicesCountQueryUtil(provinceId,startTime,endTime,list);
                    serviceCountData.setPointTypeArray(list);
                    ObjectMapper mapper = new ObjectMapper();
                    try {
                        String value = mapper.writeValueAsString(serviceCountData);
                        return value;
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }else if (pointType.equals("1")){

                }
                //时间点查询
            }else if(queryType.equals("0")){

            }
        }else if (provinceId.contains("|")){
            //按多省查
            //时间段查询
            if(queryType.equals("1")) {
                String[] split = provinceId.split("\\|");
                String queryValue1 = jsonData.getString("queryValue1");
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime = DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);
                String pointType = jsonData.getString("pointType");
                //识别点统计维度
                if (pointType.equals("0")) {
                    if (split.length > 0 && split != null) {
                        for (String province : split) {
                            servicesCountUtil.servicesCountQueryUtil(province, startTime, endTime, list);
                        }
                        serviceCountData.setPointTypeArray(list);
                        ObjectMapper mapper = new ObjectMapper();
                        try {
                            String value = mapper.writeValueAsString(serviceCountData);
                            return value;
                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                        }
                    }
                } else if (pointType.equals("1")) {

                }
                //时间点查询
            }else if(queryType.equals("0")){

            }
        }


        return null;
    }
}
